package com.longnuoqi.mydoctor.utils;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.longnuoqi.mydoctor.adapter.ViewPagerAdapter;
import com.yunhe.mydoctor.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lenovo on 2015/12/25.
 */
public class ViewPagerUtil {
    private Context context;
    private ViewPager viewPager;
    private ViewPagerAdapter adapter;
    private int[] pics;
    private List<View> viewList;

    private ImageView[] points;//记录当前选中的位置的图片
    private int currentIndex;//当前位置

    public void viewPagerUtil(Context context, View view, ViewPager viewPager, int[] pics) {
        this.context = context;
        this.viewPager = viewPager;
        this.pics = pics;
        initData(view);

    }

    private void initData(View v) {
        viewList = new ArrayList<View>();
        //定义一个布局对象，并设置了width和height
        LinearLayout.LayoutParams mParams =
                new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        for (int i = 0; i < pics.length; i++) {
            ImageView view = new ImageView(context);
            view.setLayoutParams(mParams);//防止填充的图片，将整个屏幕填满
            view.setScaleType(ImageView.ScaleType.FIT_XY);
            //加载图片
            view.setImageResource(pics[i]);
            viewList.add(view);
        }
        adapter = new ViewPagerAdapter(viewList);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(onPageChangeListener);

        initPoint(v);
    }

    private void initPoint(View view) {
        LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.linear_of_viewpager);
        points = new ImageView[pics.length];
        for (int i = 0; i < points.length; i++) {
            points[i] = (ImageView) linearLayout.getChildAt(i);
            points[i].setEnabled(true);//将每个点都变成灰色
            points[i].setOnClickListener(onClickListener);
            points[i].setTag(i);
        }
        currentIndex = 0;//设置默认页面为0；
        points[currentIndex].setEnabled(false);//被选中，颜色变为白色
    }

    private void setCurrentView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }
        viewPager.setCurrentItem(position);//根据当前位置获得viewpager的对应Item
    }

    private void setCurrentPoint(int position) {
        if (position < 0 || position > pics.length - 1 || currentIndex == position) {
            return;
        }
        points[position].setEnabled(false);
        points[currentIndex].setEnabled(true);
        currentIndex = position;
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int i = (Integer) v.getTag();//得到每个圆点图片的索引值
            setCurrentView(i);
            setCurrentPoint(i);
        }
    };


    private ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            setCurrentPoint(position);

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

}
